//
//  FiltrateSecondView.m
//  TerritoryShopkeeper
//
//  Created by xiaowu on 2017/11/28.
//  Copyright © 2017年 ZDQK. All rights reserved.
//

#import "FiltrateSecondView.h"

static CGFloat commonOffset = 10;
static NSInteger buttonTag = 123123;

@interface FiltrateSecondView ()

@property (nonatomic, strong) NSMutableArray *arr;
@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UIView *topLineView;
@property (nonatomic, strong) UIView *bottomLineView;

@end

@implementation FiltrateSecondView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self configure];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configure];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.scrollView.frame = self.bounds;
    self.topLineView.frame  = CGRectMake(0, 0, self.bounds.size.width, 1);
    self.bottomLineView.frame = CGRectMake(0, self.bounds.size.height - 1, self.bounds.size.width, 1);
}

- (void)configure {
    _arr = [NSMutableArray array];
    [self addSubview:self.scrollView];
    [self addSubview:self.topLineView];
    [self addSubview:self.bottomLineView];
}

- (void)setValuesWithTitles:(NSArray <FiltrateSecondModel *> *)arr {
    self.arr = [NSMutableArray arrayWithArray:arr];
    
    CGFloat leftOffset = commonOffset;
    CGFloat width = ScreenWidth * 0.25;
    for (NSInteger i = 0; i < arr.count; i++) {
        FiltrateSecondModel *secondModel = [arr objectAtIndex:i];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(leftOffset, commonOffset, width - 2 * commonOffset, 44 - 20);
        [button setCircleWithColor:nil borderWidth:0 cornerRadius:5];
        
        [button setTitle:secondModel.title forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [button setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHex:0xF0F2F5 alpha:1.0]] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageWithColor:[UIColor ThemeColor]] forState:UIControlStateSelected];
        [button setTitleColor:[UIColor blackTextColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        
        [button setTitleColor:[UIColor blackTextColor] forState:UIControlStateNormal];
        button.titleLabel.font = TextPingFangFontSize(12);
        
        [self.scrollView addSubview:button];
        button.tag = buttonTag + i;
        leftOffset += width;
    }
    self.scrollView.contentSize = CGSizeMake(width * arr.count, self.height);
}

- (void)buttonClick:(UIButton *)sender {
    if (self.block) {
        sender.selected = !sender.isSelected;
        self.block(sender.tag - buttonTag);
    }
}

#pragma mark - Getters
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
    }
    return _scrollView;
}

- (UIView *)topLineView {
    if (!_topLineView) {
        _topLineView = [[UIView alloc] init];
        _topLineView.backgroundColor = [UIColor grayE5Color];
    }
    return _topLineView;
}

- (UIView *)bottomLineView {
    if (!_bottomLineView) {
        _bottomLineView = [[UIView alloc] init];
        _bottomLineView.backgroundColor = [UIColor grayE5Color];
    }
    return _bottomLineView;
}


@end
